<?php
namespace Admin\Controller;

use Common\Model\WebposLocalStoreRoleMenuModel;
use Common\Model\WebposLocalStoreRoleModel;
use Think\Controller;


class BaseController extends Controller {
    protected $_arr;
    public $page_num;
    public function __construct(){
        parent::__construct();
        $this->page_num = C('PAGE_NUM');
        $this->isLogin();
        $menu = $this->power();
        $nav_menu=$this->navMenu();
        $this->assign(['menu' => $menu,'nav_menu' => $nav_menu]);
        return ['menu' => $menu,'nav_menu' => $nav_menu];
    }
    public function _showDisplay($string='') {
        $this->assign($this->_arr);
        $this->display($string);
    }

    /**
     * 判断是否登陆
     */
    public function isLogin(){
        $userInfo = session('userInfo');
        if (empty($userInfo)) {
            $this->redirect('/admin/login/index');
        }
    }

    /**
     * 判断用户权限
     */
    public function power(){
        $userInfo = session('userInfo');
        if ($userInfo['type'] == 2) {
//            $role = new WebposLocalStoreRoleModel();
//            $data = ['store_login_id' => $userInfo['id'], 'local_id' => $userInfo['local_id']];
//            $roleMsg = $role->where($data)->find(); // 查询角色信息

            // 查询角色信息
            $userRole = M('webpos_local_store_user_role');
            $roleMsg = $userRole->where(['store_login_id' => $userInfo['id'], 'local_id' => $userInfo['local_id']])->find();

            $roleMenu = new WebposLocalStoreRoleMenuModel();
            // 角色左链接查询菜单信息
            $sql = "select b.* from sdb_webpos_local_store_role_menu a LEFT JOIN sdb_webpos_local_store_menu b on a.menu_id = b.id WHERE a.role_id =  $roleMsg[role_id]";
            $roleMenuMsg = $roleMenu->query($sql);

            // 判断是否有权限，权限到控制器
            $variable = false;
            foreach ($roleMenuMsg as $v) {
                if ($v['group'] == MODULE_NAME && $v['controller'] == CONTROLLER_NAME) {
                    $variable = true;
                }
            }
            if ($variable === false) {
                $this->error('没有权限');
            }
            return $roleMenuMsg;
        }
    }

    public function navMenu(){
        $userInfo = session('userInfo');
        if ($userInfo['type'] == 2) {
//            $role = new WebposLocalStoreRoleModel();
//            $data = ['store_login_id' => $userInfo['id'], 'local_id' => $userInfo['local_id']];
//            $roleMsg = $role->where($data)->find(); // 查询角色信息

            // 查询角色信息
            $userRole = M('webpos_local_store_user_role');
            $roleMsg = $userRole->where(['store_login_id' => $userInfo['id'], 'local_id' => $userInfo['local_id']])->find(); // 查询用户的角色id


            $roleMenu = new WebposLocalStoreRoleMenuModel();
            // 角色左链接查询菜单信息
            $sql = "select b.* from sdb_webpos_local_store_role_menu a LEFT JOIN sdb_webpos_local_store_menu b on a.menu_id = b.id WHERE a.role_id =  $roleMsg[role_id] GROUP BY b.nav_menu_id";
            $roleMenuMsg = $roleMenu->query($sql);

            for ($i =0; $i<count($roleMenuMsg); $i ++ ) {
                $m = M('webpos_local_store_nav_menu');
                $nav_menu[] = $m->where(['id' => $roleMenuMsg[$i]['nav_menu_id']])->find();
            }
//            var_dump($nav_menu);exit;
            return $nav_menu;
        }
    }

    public function __destruct(){
        parent::__destruct();
    }

}